package com.globaldom.web.locations;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.globaldom.model.CivResource;
import com.globaldom.model.PersonLocation;
import com.globaldom.utils.Constants;
import com.globaldom.utils.MemCacheManager;
import com.globaldom.utils.PMF;
import com.globaldom.utils.SessionManager;

@Controller
@RequestMapping(value = "/addpersonlocation")
public class LocationAddController {

	private static final Logger log = Logger.getLogger(LocationAddController.class.getName());

	public LocationAddController() {
	}

	// FIXME! Move this to a Task!!
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, String> addLocation(@RequestParam float lat, @RequestParam float lon, @RequestParam int terdistance,
			HttpSession session, HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		
		String personID = SessionManager.getID(request, session);
		if(null==personID) 
			{
			map = new HashMap<String, String>();
			map.put(Constants.STATUS, "-ER");
			map.put(Constants.CODE, "401");
			return map;
			}

		PersonLocation location = new PersonLocation();

		PersistenceManager pmf = PMF.get().getPersistenceManager();
		try {

	
			// FIXME need basic calculations to make sure that distance is not
			// being manipulated
			if (terdistance < 2) {
				terdistance = 2;
			}

			Query query = pmf.newQuery(CivResource.class, "person==pid");
			query.declareParameters("String pid");
			List<CivResource> res = ((List<CivResource>) query.execute(personID));

			for (CivResource civResource : res) {
				if (civResource.getResType() > civResource.GOLD)
					continue;
				int cost = terdistance * (civResource.getResType() + 1) * 10;
				if (cost < 100) {
					cost = 100;
				}
				long resAvail = civResource.getResAvailable();
				if (cost > resAvail) {
					map.put(Constants.STATUS, "-ER");
					map.put(Constants.CODE, "201");
					return map;
				}
				civResource.setResAvailable(resAvail - cost);
				// res.add(civResource);
			}

			location.setPerson(personID);
			location.setLatitude(lat);
			location.setLongitude(lon);
			location.setDistance(terdistance);
			location.setDateCreated(new Date());
			location.setLocationType(Constants.LocationType.SATELLITE.ordinal());
			PersistenceManager pm = PMF.get().getPersistenceManager();
			pm.makePersistent(location);
			pm.close();
			// Remove from cache
			MemCacheManager.removeCivics(personID);
			map.put(Constants.STATUS, "+OK");
			map.put(Constants.CODE, "200");

		} finally {
			pmf.close();
		}
		map.put(Constants.DATA, lat + "/" + lon + "/" + location.getId());
		MemCacheManager.addLocation(personID, location);
		return map;
	}

	// initializing method
	@RequestMapping(method = RequestMethod.GET)
	public String getCreateForm(Model model, HttpSession session) {
		return "redirect:/country";
	}

}
